package com.vogue.supplymall.shop.common;

import com.vogue.gear.memory.provider.IMemProvider;
import com.vogue.supplymall.common.constant.ConstantBean;
import com.vogue.supplymall.shop.domain.model.Administrativearea;
import com.vogue.supplymall.shop.domain.service.AdministrativeareaService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

/**
 * The type Mem utils.
 * 拿取缓存中的区域信息
 *
 * @aouthor: zhangliang
 * @date: Create in 2017-12-19
 */
@Component
public class IMemUtils implements Serializable {
    @Resource(name = "memProvider")
    private IMemProvider<Object> memProvider;
    @Resource
    private AdministrativeareaService administrativeareaService;
    /*使用缓存开关 1：关闭*/
    @Value("${cacheOnOff}")
    private final static Integer onOff = 1;

    /**
     * Gets area map.
     * 返回检索用的map
     *
     * @param provinceId the province id
     * @return the area map
     */
    public Map<String, String> getAreaMap(String provinceId) {
        // 获得全部省的行政区域信息
        List<Administrativearea> areas = getArea(ConstantBean.AREA_LEVEL_2);
        Function<List<Administrativearea>, Optional<Map<String, String>>> areaApply = (value) -> value
                .stream()
                .filter(area -> area.getId().equals(new Long(provinceId)))
                .map((area) -> {
                    Map<String, String> map = new HashMap<>();
                    map.put("4", area.getId().toString());
                    map.put("2", area.getAreacode());
                    return map;
                })
                .findFirst();
        Optional<Map<String, String>> result = areaApply.apply(areas);
        if (result.isPresent()) {
            return result.get();
        }
        return null;
    }

    /**
     * Gets area.
     * 根据级别返回区域
     * 如果缓存有数据从缓存拿取
     * ，如果没有查询数据库。
     * 2：拿取省，3：拿取市
     *
     * @param level the level
     * @return the area
     */
    public List<Administrativearea> getArea(Integer level) {
        List<Administrativearea> value = null;
        if(0 == onOff) {
            if (ConstantBean.AREA_LEVEL_2.equals(level)) {
                value = (List<Administrativearea>) memProvider.get(ConstantBean.ADMINISTRATIVE_AREA_PROVINCE_KEY);
            } else if (ConstantBean.AREA_LEVEL_3.equals(level)) {
                value = (List<Administrativearea>) memProvider.get(ConstantBean.ADMINISTRATIVE_AREA_CITY_KEY);
            }
        }

        if (null == value) {
            value = setArea(level);
        }
        return value;
    }

    private List<Administrativearea> setArea(Integer level) {
        List<Administrativearea> lists = null;
        if (ConstantBean.AREA_LEVEL_3 == level) {
            level = ConstantBean.AREA_LEVEL_3;
            lists = administrativeareaService.findAreaByLevel(ConstantBean.AREA_LEVEL_3);
        } else if (ConstantBean.AREA_LEVEL_2 == level) {
            level = ConstantBean.AREA_LEVEL_2;
            lists = administrativeareaService.findAreaByLevel(level);
        }

        if(0 == onOff) {
            switch (level) {
                case 1:
                    memProvider.put(ConstantBean.ADMINISTRATIVE_AREA_PROVINCE_KEY, lists);
                    break;
                case 2:
                    memProvider.put(ConstantBean.ADMINISTRATIVE_AREA_CITY_KEY, lists);
                    break;
            }
        }

        return lists;
    }

    /**
     * Gets city shop.
     * 从缓存中拿取同城店铺
     *
     * @param city the city
     * @return the city shop
     */
    public Object getCityShop(String city) {
        return memProvider.get(city + ConstantBean.CITY_SHOP);

    }

    /**
     * Sets city shop.
     * 同城店铺写入缓存
     *
     * @param city  the city
     * @param lists the lists
     */
    public void setCityShop(String city, Object lists) {
        memProvider.put(city + ConstantBean.CITY_SHOP, lists);
    }
}